public with sharing class AddCallPlanControl
{
    //Activity Management
    public String IdSelect{get;set;}
    public Integer TotalChecked{get;set;}
    public Integer TotalAdded{get;set;}
    public String msg{get;set;}
    public Hospital__c HospitalInfo{get;set;}
    public List<AddHCPItemControl> HCPItemList{get;set;}
    public List<AddCallPlanItemControl> CallPlanList{get;set;}
    public List<AddCallPlanItemControl> OtherList{get;set;}
    public String RTSelect{get;set;}
    public String wtSelect{get;set;}
    public String wkString{get;set;}
    public boolean showmsg{get;set;}
    public boolean returnFlag = false;
    public Boolean SDisplay{
    get
    {
        if((wtSelect == '3')||(wtSelect == '31')||(wtSelect == '4')||(wtSelect == '41')||
        (wtSelect == '5')||(wtSelect == '6')||(wtSelect == '7')||(wtSelect == '8')||(wtSelect == '9'))  
        {
            SDisplay = true;
            
        }
        else{
            SDisplay  = false;
        }
        return SDisplay;
    }set;} 
    
    public List<String> IdList{
        get{
            String IdFrist = 'HospitalDetails';
            String IdSecond = 'HCPList';
            List<String> returnList = new List<String>();
            returnList.add(IdFrist);
            returnList.add(IdSecond);
            return returnList;
        }
        set;
    }
    //get url id
    public String UrlId
    {
        get{        
                return System.currentPageReference().getParameters().get('id');     
           }  
        set;
    }    
    public CallPlan__c tDate{
        get{
            if(tDate== null){
                tDate= new CallPlan__c();             
            }
            tDate.CallPlanDate__c = Date.today();
            return tDate;
        }
        set;    
    }
    
    public CallPlan__c cpDate{
        get{
            if(cpDate== null){
                cpDate= new CallPlan__c();   
                List<MonthlyCallPlan__c> cmpl = [Select Id,Name,PlanMonth__c,PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
                if(cmpl.size() > 0){
                    String month_ss = cmpl.get(0).PlanMonth__c;
                    String month_s = month_ss.replace('月','');
                    Integer month_i = integer.valueof(month_s);
                    Integer year_i = integer.valueof(cmpl.get(0).PlanYear__c);
                    cpDate.CallPlanDate__c  = date.newinstance(year_i,month_i,1);
                
                }          
            }    

            return cpDate;
        }
        set;    
    }
    

    public String opflag
    {
        get{        
                return System.currentPageReference().getParameters().get('opflag');     
           }  
        set;
    }

    public List<SelectOption> WorkType
    {
        get
        {
            if(WorkType == null)
                WorkType = new List<SelectOption>();
            else
                WorkType.clear();
            
            WorkType.add(new SelectOption('1','无'));
            WorkType.add(new SelectOption('2','拜访'));
            WorkType.add(new SelectOption('3','本地消费者教育活动'));
            WorkType.add(new SelectOption('31','全国消费者教育活动'));
            WorkType.add(new SelectOption('4','学术沙龙'));
            WorkType.add(new SelectOption('41','全国医护人员教育活动'));
            WorkType.add(new SelectOption('5','MJN例会'));
            WorkType.add(new SelectOption('6','培训'));
            WorkType.add(new SelectOption('7','年休假')); 
            WorkType.add(new SelectOption('8','病假')); 
            WorkType.add(new SelectOption('9','其他')); 
            
            return WorkType;                      
        }
        set;
    }


    
    //option for hospital
    public List<SelectOption> options 
    {
        get
        {
            if(options == null)
                options = new List<SelectOption>();
            else
                options.clear();
            
            if((wtSelect == '3')||(wtSelect == '31')||(wtSelect == '4')||(wtSelect == '41')||
            (wtSelect == '5')||(wtSelect == '6')||(wtSelect == '7')||(wtSelect == '8')||(wtSelect == '9'))  
            {
                options.add(new SelectOption('','无'));
                if(HCPItemList != null){
                    HCPItemList.clear();
                }
                if(HospitalInfo != null){
                    HospitalInfo = null;
                }
                
                return options;
            } 
            options.add(new SelectOption('','无'));//first Add none value
          //  List<Hospital__c> HosptialList = [Select Id,Name,Active__c,class__c,effectivedate__c,approvaltype__c,lastvisiteddate__c From
          //      Hospital__c where OwnerId  =: UserInfo.getUserId()]; //get hospital fix by 12-31
            List<Hospital__c> HosptialList = [Select Id,ownerid,Name,Active__c,class__c,effectivedate__c,approvaltype__c,lastvisiteddate__c From
                Hospital__c limit 8000];        
            for(Hospital__c hp:HosptialList )
            {               
                if((hp.Active__c == true)&&(hp.ownerid == UserInfo.getUserId())){
                    options.add(new SelectOption(hp.Id,hp.Name));
                }
                if((hp.Active__c == true)&&(hp.ownerid != UserInfo.getUserId())&&(hp.approvaltype__c == '改变MSR')&&(hp.effectivedate__c <= cpDate.CallPlanDate__c))
                {
                    options.add(new SelectOption(hp.Id,hp.Name));
                }
                
                if((hp.effectivedate__c != null)&&(cpDate.CallPlanDate__c != null)&&(opflag == '1')){
                   if((hp.effectivedate__c <= cpDate.CallPlanDate__c)&&(hp.Active__c == false)&&(hp.approvaltype__c == '新增医院'))
                   {
                       options.add(new SelectOption(hp.Id,hp.Name));
                   }
               }
               
            }
            
            return options ;
        }
        set;
    }
   
    public AddCallPlanControl()
    {
            SDisplay = true;


    } 

    
    public PageReference  actionSupport()
    {   
        if(HCPItemList == null)
            HCPItemList = new List<AddHCPItemControl>();
        else
            HCPItemList.clear();
        
        TotalChecked =null;    
        if(IdSelect != null){
            HospitalInfo = [Select class__c, lastvisiteddate__c From Hospital__c where Id  =: IdSelect];             
        }
        
        List<HCP__c> HCPList = [Select approvaltype__c,class1__c,effectivedate__c,class2__c,class3__c,Active__c,lastvisiteddate__c,
            approvalstatus__c,Id,Name,proftitle__c,Dept__c From HCP__c Where Hospital__c =: IdSelect order by dept__c];
        for(HCP__c hcp:HCPList )
        {
            if(hcp.Active__c == true)
            {
                AddHCPItemControl add = new AddHCPItemControl();
                add.HCP = hcp;
            
                if(hcp != null){
                    HCPItemList.add(add);                
                }   
            }
            
            if((hcp.effectivedate__c != null)&&(cpDate.CallPlanDate__c != null)&&(opflag == '1')){
                if((hcp.effectivedate__c <= cpDate.CallPlanDate__c)&&(hcp.Active__c == false)&&(hcp.approvaltype__c == '新增医护人员'))
                {
                    AddHCPItemControl add = new AddHCPItemControl();
                    add.HCP = hcp;
            
                    if(hcp != null){
                        HCPItemList.add(add);                
                    }  
                }
            }            
        
        }
        
        return null;
    }
    
    public List<SelectOption> RTOptions
    {
        get
        {
            if(RTOptions == null)
                RTOptions= new List<SelectOption>();
            else
                RTOptions.clear();
                
            List<recordtype> saloonIdl = [select id from recordtype where sobjecttype=:'CallPlan__c' and name=:'日常拜访计划']; //012O00000008UXgIAM
            recordtype saloonId = saloonIdl.get(0);
            RTOptions.add(new SelectOption(saloonId.Id,'日常拜访'));
            return RTOptions;
        }
        set;
    }  
    
    public PageReference UpdateRecord()
    {
        if(UrlId == null){
            pageReference callhome = new pageReference('/a0e/o');
            return callhome;   
        }

        Boolean haveThisOne = false;
        Boolean havehcp = false;
        Boolean delhcp = false;                

        returnFlag = true;      
        Date Dtoday = Date.today();     
        Integer Imonth = Dtoday.month();           
        Integer Iyear = Dtoday.year();
        String Smonth = String.valueOf(Imonth);
        String Syear = String.valueOf(Iyear);
        string stringDate = Syear + '-' + Smonth + '-' + '1';
        Date myDate = date.valueOf(stringDate);
   
        if(cpDate.CallPlanDate__c >= Date.today())
        {
            msg = null;
            showmsg=false;
        }                  
       List<MonthlyCallPlan__c> moncalp = [Select Id,Name,PlanMonth__c,PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
       if(moncalp.size() > 0){
           String mon_t = moncalp.get(0).PlanMonth__c.replace('月','');
           Integer mon_i = integer.valueof(mon_t);
           Integer yea_i = integer.valueof(moncalp.get(0).PlanYear__c);
           if((mon_i != cpDate.CallPlanDate__c.month())||(yea_i != cpDate.CallPlanDate__c.year()))
           {
               msg = '日期选择有误，您进入的是' + moncalp.get(0).Name;
               showmsg=true;
               return null;
           }
       } 
              
       if(wtSelect == '2'){
           wkString = '拜访';
       }
       if(wtSelect == '3'){
           wkString = '本地消费者教育活动';
       }
       if(wtSelect == '31'){
           wkString = '全国消费者教育活动';
       }
       if(wtSelect == '4'){
           wkString = '学术沙龙';
       }
       if(wtSelect == '41'){
           wkString = '全国医护人员教育活动';
       }
       if(wtSelect == '5'){
           wkString = 'MJN例会';
       }       
       if(wtSelect == '6'){
           wkString = '培训';
       }
       if(wtSelect == '7'){
           wkString = '年休假';
       }       
       if(wtSelect == '8'){
           wkString = '病假';
       }
       if(wtSelect == '9'){
           wkString = '其他';
       }                    
       if(wkString == null)
       {
           return null;
       }
       
       if((wkString == '本地消费者教育活动')||(wkString == '全国消费者教育活动')||(wkString == '学术沙龙')||(wkString == '全国医护人员教育活动')||
           (wkString == 'MJN例会')||(wkString == '培训')||(wkString == '年休假')||(wkString == '病假')||(wkString == '其他'))
       {
            if(OtherList == null){
                OtherList = new List<AddCallPlanItemControl>();
            }                       
            AddCallPlanItemControl acpi = new AddCallPlanItemControl();
            acpi.CallPlan.CallWorkingtype__c = wkString;
            acpi.CallPlan.CallPlanTime__c = 'AM(9am-12am)';
            acpi.CallPlan.CallExecutionstatus__c = '否';
            OtherList.add(acpi);
            return null;                                       
        }     
                
        if((CallPlanList == null) && (HCPItemList != null)){
            CallPlanList = new List<AddCallPlanItemControl>();
        }

        if((IdSelect == '无')||(IdSelect == null)){
            return null;
        }
        if(HCPItemList == null){
          return null;
        }            
        if(HCPItemList.size() <= 0)
        {
            return null;
        }
        Integer num = 0;
        for(AddHCPItemControl addHCP:HCPItemList){ 
            delhcp = false;
            if(addHCP.IsCheck == true){
                for(AddCallPlanItemControl acpic:CallPlanList)
                {                   
                    if(addHCP.HCP.Id == acpic.CallPlan.HCP__c){
                        delhcp  = true; 
                        if(havehcp == true){
                            msg = addHCP.HCP.name + ','+msg;
                            showmsg=true;
                        }
                        else{
                            msg = addHCP.HCP.name +'已在计划拜访日期中';
                            showmsg=true;
                        }
                        havehcp = true;
                        delhcp = true;
                    }
                }           
            }
            // select from sql to look up reply
            if(addHCP.IsCheck == true){
                List<CallPlan__c> reply_cp = new List<CallPlan__c>();
                if(opflag == '1'){
                    reply_cp = [Select Id,Name From CallPlan__c Where HCP__c =: addHCP.HCP.Id and
                         CallPlanDate__c =: cpDate.CallPlanDate__c];
                }
                else{
                    reply_cp = [Select Id,Name From CallPlan__c Where HCP__c =: addHCP.HCP.Id and
                         CallPlanDate__c =: cpDate.CallPlanDate__c];
                }
                if(reply_cp.size()>0){
                        delhcp  = true; 
                        if(havehcp == true){
                            msg = addHCP.HCP.name + ','+msg;
                            showmsg=true;
                        }
                        else{
                            msg = addHCP.HCP.name +'已在计划拜访日期中';
                            showmsg=true;
                        }
                        havehcp = true;
                        delhcp = true;               
                }
            
            }                     
                         
            if((addHCP.IsCheck == true)&&(delhcp == false)){
                CallPlan__c cp = new CallPlan__c();              
                cp.HCP__c = addHCP.HCP.Id;
                AddCallPlanItemControl acpi = new AddCallPlanItemControl();
                acpi.CallPlan = cp;
                acpi.CallPlan.CallWorkingtype__c = wkString; 
                acpi.CallPlan.CallPlanTime__c = 'AM(9am-12am)';
                acpi.level = '';
                if(addHCP.HCP.class1__c != null){
                    acpi.level = addHCP.HCP.class1__c;
                }
                if(addHCP.HCP.class2__c != null){
                    acpi.level = acpi.level + addHCP.HCP.class2__c;
                }
                if(addHCP.HCP.class3__c != null){
                    acpi.level = acpi.level + addHCP.HCP.class3__c;
                }
                acpi.department = addHCP.HCP.Dept__c;
                acpi.CallPlan.CallExecutionstatus__c = '否';  
                num ++;          
                CallPlanList.add(acpi);                       
            }
                      
        }
        
        TotalAdded = CallPlanList.size();
        TotalChecked = num;
        if(havehcp == false){
            msg = null;
            showmsg=false;
        }
        else{
            havehcp = true;
        }
        return null;        
    }
         
    public PageReference DeleteRecord()
    {
        List <Integer> delnum = new List <Integer>();
        List<AddCallPlanItemControl> CopyList = new List<AddCallPlanItemControl>();
        if(CallPlanList != null){
            for(AddCallPlanItemControl delCP:CallPlanList)
            {
                if(delCP.IsCheck == false)
                {
                    CopyList.add(delCP);
                }
            }
            CallPlanList.clear();
            CallPlanList = CopyList;            
        } 
        TotalAdded = CallPlanList.size();
        return null;
    }
    
    
    public PageReference DeleteOther()
    {
        List <Integer> delnumO = new List <Integer>();
        List<AddCallPlanItemControl> CopyOList = new List<AddCallPlanItemControl>();
        if(OtherList != null){
            for(AddCallPlanItemControl delCPO:OtherList)
            {
                if(delCPO.IsCheck == false)
                {
                    CopyOList.add(delCPO); 
                }
            }
            OtherList.clear();
            OtherList = CopyOList;    
        }
        return null;
    }    
    
    public PageReference Cancel()
    {
        //go back to region list page
        pageReference pr = new pageReference('/'+ UrlId);     
        pr.setredirect(true);      
        return pr;
    }    
        
    public PageReference Save()
    { 
        if(UrlId == null){
            pageReference callhome = new pageReference('/a0e/o');
            return callhome;   
        }
        if(returnFlag == false){
            pageReference pr = new pageReference('/'+ UrlId);     
            pr.setredirect(true);      
            return pr;       
        }
        
         List<CallPlan__c> SaveCPList = new List<CallPlan__c>();      
        try{     
        if((cpDate.CallPlanDate__c == null)&&(CallPlanList == null)){
            pageReference pr = new pageReference('/'+ UrlId);     
            pr.setredirect(true);      
            return pr;
        }
        if((cpDate.CallPlanDate__c == null)&&(CallPlanList != null)){
            msg = '日期未提写';
            showmsg=true;
            return null;
        }
       List<MonthlyCallPlan__c> moncalp = [Select Id,Name,PlanMonth__c,PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
       if(moncalp.size() > 0){
           String mon_t = moncalp.get(0).PlanMonth__c.replace('月','');
           Integer mon_i = integer.valueof(mon_t);
           Integer yea_i = integer.valueof(moncalp.get(0).PlanYear__c);
           if((mon_i != cpDate.CallPlanDate__c.month())||(yea_i != cpDate.CallPlanDate__c.year()))
           {
               msg = '日期选择有误，您进入的是' + moncalp.get(0).Name;
               showmsg=true;
               return null;
           }
       }         
        
        if(cpDate.CallPlanDate__c <= Date.today() )
        {
             msg = '不能添加明天之前的拜访计划';
             showmsg=true;
             return null;
        }

        if(cpDate.CallPlanDate__c > Date.today())
        {
            msg = null;
            showmsg=false;
        }
        if(CallPlanList != null){
        if(CallPlanList.size() > 0){       
            for(AddCallPlanItemControl saveCP:CallPlanList){
                if((saveCP.CallPlan.CallMessage__c == null)||(saveCP.CallPlan.CallPurpose__c == null)){
                    msg = '拜访目的和传递信息必须填写！';
                    return null;
                }
            }
        }
        }
        
        if((CallPlanList != null)||(OtherList != null)){
        if(CallPlanList != null){
        for(AddCallPlanItemControl saveCP:CallPlanList)
        {

            saveCP.CallPlan.Name = 'AutoName';
            if(saveCP.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCP.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
            {
                Time starttime = Time.newInstance(14, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCP.CallPlan.CallPlanTime__c == 'AllDay')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }   
            if(cpDate.CallPlanDate__c != null){
                saveCP.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
            }
            
            saveCP.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCP.CallPlan.PlanType__c = '计划内';
                   
            if(saveCP.CallPlan != null){
                SaveCPList.add(saveCP.CallPlan); 
                //insert saveCP.CallPlan;
            }           
        }
        }
        if(OtherList != null){
        for(AddCallPlanItemControl saveCPO:OtherList)
        {

            saveCPO.CallPlan.Name = 'AutoName';
            if(saveCPO.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCPO.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
            {
                Time starttime = Time.newInstance(14, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCPO.CallPlan.CallPlanTime__c == 'AllDay')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }   
            if(cpDate.CallPlanDate__c != null){
                saveCPO.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
            }
            
            saveCPO.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCPO.CallPlan.PlanType__c = '计划内';
                   
            if(saveCPO.CallPlan != null){
                SaveCPList.add(saveCPO.CallPlan); 
                //insert saveCPO.CallPlan;
            }           
        }
        }
            insert SaveCPList; 
            //go back to region list page
            pageReference pr = new pageReference('/'+ UrlId);     
            pr.setredirect(true);      
            return pr;
        }
        return null;
        }
        catch (DmlException e) {
            ApexPages.addMessages(e);
            return null;
         } 
        
    }

    public PageReference SaveAndNew()
    {
        boolean insertFlag = false;
        TotalChecked = 0;
        TotalAdded = 0;
        if(UrlId == null){
            pageReference callhome = new pageReference('/a0e/o');
            return callhome;   
        }
        if(returnFlag == false){
            pageReference pr = new pageReference('/'+ UrlId);     
            pr.setredirect(true);      
            return pr;              
        }
        List<CallPlan__c> SaveCPList = new List<CallPlan__c>();               
        try
        { 
        if((cpDate.CallPlanDate__c == null)&&(CallPlanList == null)){
            return null;
        }
       
        if((cpDate.CallPlanDate__c == null)&&(CallPlanList != null)){
            msg = '日期未填写';
            showmsg=true;
            return null;
        }
       List<MonthlyCallPlan__c> moncalp = [Select Id,Name,PlanMonth__c,PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
       if(moncalp.size() > 0){
           String mon_t = moncalp.get(0).PlanMonth__c.replace('月','');
           Integer mon_i = integer.valueof(mon_t);
           Integer yea_i = integer.valueof(moncalp.get(0).PlanYear__c);
           if((mon_i != cpDate.CallPlanDate__c.month())||(yea_i != cpDate.CallPlanDate__c.year()))
           {
               msg = '日期选择有误，您进入的是' + moncalp.get(0).Name;
               showmsg=true;
               return null;
           }
       }
       
        
        if(cpDate.CallPlanDate__c <= Date.today()  )
        {
             msg = '不能添加明天之前的拜访计划';
             showmsg=true;
             return null;
        }


        if(cpDate.CallPlanDate__c > Date.today())
        {
            msg = null;
            showmsg=false;
        } 
        if(CallPlanList != null){
        if(CallPlanList.size() > 0){       
            for(AddCallPlanItemControl saveCP:CallPlanList){
                if((saveCP.CallPlan.CallMessage__c == null)||(saveCP.CallPlan.CallPurpose__c == null)){
                    msg = '拜访目的和传递信息必须填写！';
                    return null;
                }
            }
        }
        }        
        
        if((CallPlanList != null)||(OtherList != null)){
        if(CallPlanList != null){
        for(AddCallPlanItemControl saveCP:CallPlanList)
        {

            saveCP.CallPlan.Name = 'AutoName';
            if(saveCP.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCP.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
            {
                Time starttime = Time.newInstance(14, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCP.CallPlan.CallPlanTime__c == 'AllDay')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }   
            if(cpDate.CallPlanDate__c != null){
                saveCP.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
            }
            
            saveCP.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCP.CallPlan.PlanType__c = '计划内';
                   
            if(saveCP.CallPlan != null){
                SaveCPList.add(saveCP.CallPlan); 
                //insert saveCP.CallPlan;
            }           
        }
        }
        if(OtherList != null){
        for(AddCallPlanItemControl saveCPO:OtherList)
        {

            saveCPO.CallPlan.Name = 'AutoName';
            if(saveCPO.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCPO.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
            {
                Time starttime = Time.newInstance(14, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }
            if(saveCPO.CallPlan.CallPlanTime__c == 'AllDay')
            {
                Time starttime = Time.newInstance(9, 0, 0, 0);
                if(cpDate.CallPlanDate__c != null){
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
            }   
            if(cpDate.CallPlanDate__c != null){
                saveCPO.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
            }
            
            saveCPO.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCPO.CallPlan.PlanType__c = '计划内';
                   
            if(saveCPO.CallPlan != null){
                SaveCPList.add(saveCPO.CallPlan); 
                //insert saveCPO.CallPlan;
            }           
        }
        }
        insertFlag = true;
        insert SaveCPList;
        
        CallPlanList = null;
        OtherList = null;
        HCPItemList = null;
        HospitalInfo = null;
        Date addDate = cpDate.CallPlanDate__c;
        cpDate.CallPlanDate__c = addDate.addDays(1);              
        }

        IdSelect = null;
        msg = null;
        showmsg=false;
        wtSelect = '1';
        return null;
      }  
       catch (DmlException e) {
            ApexPages.addMessages(e);
            if((SaveCPList != null)&&(insertFlag == true)){
                if(SaveCPList.size() > 0)
                    delete SaveCPList;
            }
            return null;
         }
        
        
    }
    
    public PageReference SaveOutPlan()
    { 
        if(UrlId == null){
            pageReference callhome = new pageReference('/a0e/o');
            return callhome;   
        }
        if(returnFlag == false){
            pageReference pr = new pageReference('/'+ UrlId);     
            pr.setredirect(true);      
            return pr;             
        }
        
        if((cpDate.CallPlanDate__c == null)&&(CallPlanList != null)){
            msg = '日期未填写';
            showmsg=true;
            return null;
        }
                
        if(cpDate.CallPlanDate__c > date.today()){
            msg = '不能记录今天以后的计划';
            showmsg=true;
            return null;        
        } 
       List<CallPlan__c> SaveCPList = new List<CallPlan__c>();    
       List<MonthlyCallPlan__c> moncalp = [Select Id,Name,PlanMonth__c,PlanYear__c From MonthlyCallPlan__c Where Id =: UrlId];
       if(moncalp.size() > 0){
           String mon_t = moncalp.get(0).PlanMonth__c.replace('月','');
           Integer mon_i = integer.valueof(mon_t);
           Integer yea_i = integer.valueof(moncalp.get(0).PlanYear__c);
           if((mon_i != cpDate.CallPlanDate__c.month())||(yea_i != cpDate.CallPlanDate__c.year()))
           {
               msg = '日期选择有误，您进入的是' + moncalp.get(0).Name;
               showmsg=true;
               return null;
           }
       }         
                              
     try
        { 
        if(CallPlanList != null){
        if(CallPlanList.size() > 0){       
            for(AddCallPlanItemControl saveCP:CallPlanList){
                if((saveCP.CallPlan.CallMessage__c == null)||(saveCP.CallPlan.CallPurpose__c == null)){
                    msg = '拜访目的和传递信息必须填写！';
                    return null;
                }
            }
        }
        }       
        
        if((CallPlanList != null)||(OtherList != null)){
        if(CallPlanList != null){
        for(AddCallPlanItemControl saveCP:CallPlanList)
        {          
            saveCP.CallPlan.Name = 'AutoName';

            if(cpDate.CallPlanDate__c != null){
                saveCP.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
                if(saveCP.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
                {
                    Time starttime = Time.newInstance(9, 0, 0, 0);
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
                if(saveCP.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
                {
                    Time starttime = Time.newInstance(14, 0, 0, 0);
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
                if(saveCP.CallPlan.CallPlanTime__c == 'AllDay')
                {
                    Time starttime = Time.newInstance(9, 0, 0, 0);
                    saveCP.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }     
                
            }
            
            saveCP.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCP.CallPlan.PlanType__c = '计划外';
            saveCP.CallPlan.CallExecutionstatus__c = '是';
                   
            if(saveCP.CallPlan != null){
                SaveCPList.add(saveCP.CallPlan); 
                //insert saveCP.CallPlan;
            }           
        }
        }
        if(OtherList != null){
        for(AddCallPlanItemControl saveCPO:OtherList)
        {          
            saveCPO.CallPlan.Name = 'AutoName';

            if(cpDate.CallPlanDate__c != null){
                saveCPO.CallPlan.CallPlanDate__c = cpDate.CallPlanDate__c;
                if(saveCPO.CallPlan.CallPlanTime__c == 'AM(9am-12am)')
                {
                    Time starttime = Time.newInstance(9, 0, 0, 0);
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
                if(saveCPO.CallPlan.CallPlanTime__c == 'PM(2pm-5pm)')
                {
                    Time starttime = Time.newInstance(14, 0, 0, 0);
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }
                if(saveCPO.CallPlan.CallPlanTime__c == 'AllDay')
                {
                    Time starttime = Time.newInstance(9, 0, 0, 0);
                    saveCPO.CallPlan.CallDate__c =  datetime.newInstance(cpDate.CallPlanDate__c, starttime );               
                }     
                
            }
            
            saveCPO.CallPlan.Monthly_Call_Plan__c = UrlId;
            saveCPO.CallPlan.PlanType__c = '计划外';
                   
            if(saveCPO.CallPlan != null){
                SaveCPList.add(saveCPO.CallPlan); 
            }           
        }
        }
        /*modified by xiaojiang 100914  insert SaveCPList;*/    
        if(SaveCPList.size()>0)
        {
          insert SaveCPList;
        }  
       /* end modify*/
         
        //go back to region list page
        pageReference pr = page.AddCallRecord;
        pr.getParameters().put('id', UrlId);
        return pr;
        }
        return null;
       }  
       catch (DmlException e) {
            ApexPages.addMessages(e);
            if(SaveCPList.size() > 0)
                delete SaveCPList;
            return null;
         }
    }
    
    public PageReference CancelOutPlan()
    {
        //go back to region list page
        pageReference pr = page.AddCallRecord;
        pr.getParameters().put('id', UrlId);
        return pr;
    } 

    static testMethod void AddCallPlanControlTest()
    {
    
        Profile p  = [select id from profile where name = 'Sales Rep - Medical'];
        
        User u1 = new User (alias = 'sssbbb', email = 'sssbbb@isoftstone.com', emailencodingkey='UTF-8', 
                lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', Title__c = 'Sales Rep',profileid = p.id, 
                timezonesidkey = 'America/Los_Angeles', username='sssbbb@testorg1.com');
        insert u1;

    System.runAs(u1) { 
             
        MonthlyCallPlan__c mcp = new MonthlyCallPlan__c();
        mcp.PlanYear__c = '2010';
        mcp.PlanMonth__c = '12月';
        insert mcp;
        
        MonthlyCallPlan__c mcp1 = new MonthlyCallPlan__c();
        mcp1.PlanYear__c = '2011';
        mcp1.PlanMonth__c = '12月';
        insert mcp1;  
                               
                 
        CallPlan__c cp = new CallPlan__c();
        cp.name = 'a';
        cp.CallPlanTime__c = 'AM(9am-12am)';
       
        cp.Lasts__c = 3;
        cp.CallDate__c = dateTime.Now();
        cp.PlanType__c = '计划外';
        cp.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp.CallPurpose__c = '商谈专业评估';     
                     
        Hospital__c hos = new Hospital__c();
        hos.Name = 'aaaaaaaa';
        hos.Nickname__c = 'aaaaaaaaaaa';
        hos.Active__c = true;
        insert hos;
        
           /*by xiaojiang*/
        Hospital__c hos2 = new Hospital__c();
        hos2.Name = 'aaaaaaaa1';
        hos2.effectivedate__c = cp.CallDate__c.date();
        hos2.Active__c =false;
        hos2.approvaltype__c  ='新增医院';
        insert hos2;
        /*by xiaojiang 100910*/
        
        HCP__c hcp= new HCP__c();
        hcp.class1__c = '全国学术带头人(NKOL)';
        hcp.Hospital__c = hos.Id;
        hcp.Active__c = true;
        insert hcp;   
        
        cp.HCP__c = hcp.Id;
        
        /**************************/
        HCP__c hcp1= new HCP__c();
        hcp1.class1__c = '全国学术带头人(NKOL)';
        hcp1.Hospital__c = hos.Id;
        hcp1.Active__c = true;
        insert hcp1; 
        
        CallPlan__c cp1 = new CallPlan__c();
        cp1.name = 'a';
        cp1.CallPlanTime__c = 'AllDay';
       
        cp1.Lasts__c = 3;
        cp1.CallDate__c = dateTime.Now();
        cp1.PlanType__c = '计划外'; 
        cp1.HCP__c = hcp1.Id;
        cp1.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp1.CallPurpose__c = '商谈专业评估';             
        /*****************************/ 
        
        
        /**************************/
        HCP__c hcp2= new HCP__c();
        hcp2.class1__c = '全国学术带头人(NKOL)';
        hcp2.Hospital__c = hos.Id;
        hcp2.Active__c = true;
        insert hcp2; 
        
        CallPlan__c cp2 = new CallPlan__c();
        cp2.name = 'a';
        cp2.CallPlanTime__c = 'AllDay';
       
        cp2.Lasts__c = 3;
        cp2.CallDate__c = dateTime.Now();
        cp2.PlanType__c = '计划外'; 
        cp2.HCP__c = hcp2.Id;
        cp2.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp2.CallPurpose__c = '商谈专业评估';             
        /*****************************/  
        
        
        /**************************/
        HCP__c hcp3= new HCP__c();
        hcp3.class1__c = '全国学术带头人(NKOL)';
        hcp3.Hospital__c = hos.Id;
        hcp3.Active__c = true;
        insert hcp3; 
                
        CallPlan__c cp3 = new CallPlan__c();
        cp3.name = 'a';
        cp3.CallPlanTime__c = 'AllDay';
       
        cp3.Lasts__c = 3;
        cp3.CallDate__c = dateTime.Now();
        cp3.PlanType__c = '计划外'; 
        cp3.HCP__c = hcp3.Id;
        cp3.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp3.CallPurpose__c = '商谈专业评估';             
        /*****************************/  
        
        /**************************/
                HCP__c hcp4= new HCP__c();
        hcp4.class1__c = '全国学术带头人(NKOL)';
        hcp4.Hospital__c = hos.Id;
        hcp4.Active__c = true;
        insert hcp4; 
        
        CallPlan__c cp4 = new CallPlan__c();
        cp4.name = 'a';
        cp4.CallPlanTime__c = 'AllDay';
       
        cp4.Lasts__c = 3;
        cp4.CallDate__c = dateTime.Now();
        cp4.PlanType__c = '计划外'; 
        cp4.HCP__c = hcp4.Id;
        cp4.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp4.CallPurpose__c = '商谈专业评估';             
        /*****************************/  
        
        
       /**************************/
        HCP__c hcp5= new HCP__c();
        hcp5.class1__c = '全国学术带头人(NKOL)';
        hcp5.Hospital__c = hos.Id;
        hcp5.Active__c = true;
        insert hcp5;
        
        CallPlan__c cp5 = new CallPlan__c();
        cp5.name = 'a';
        cp5.CallPlanTime__c = 'AM(9am-12am)';
        
        /*create by xiaojiang 100916*/
       
         CallPlan__c cp7 = new CallPlan__c();
        cp7.name = 'a';
        cp7.CallPlanTime__c = 'AllDay';
        
          CallPlan__c cp8 = new CallPlan__c();
        cp8 .name = 'a';
        cp8 .CallPlanTime__c ='PM(2pm-5pm)';
        /*end create*/
        
       
        cp5.Lasts__c = 3;
        cp5.CallDate__c = dateTime.Now();
        cp5.PlanType__c = '计划外'; 
        cp5.HCP__c = hcp5.Id;
        cp5.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp5.CallPurpose__c = '商谈专业评估';           
        /*****************************/       
        /* create by xiaojiang 100914*/
          HCP__c hcp6= new HCP__c();
        hcp6.class1__c = '全国学术带头人1(NKOL)';
        hcp6.Hospital__c = hos.Id;
        hcp6.Active__c = true;
        insert hcp6;
        
        CallPlan__c cp6 = new CallPlan__c();
        cp6.name = 'a';
        cp6.CallPlanTime__c = 'AllDay';
       
        cp6.Lasts__c = 3;
        cp6.CallDate__c = datetime.now().addDays(2);
        cp6.PlanType__c = '计划外'; 
        cp6.HCP__c = hcp6.Id;
        cp6.CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）'; 
        cp6.CallPurpose__c = '商谈专业评估';  
        /*end create*/
                     

        AddCallPlanItemControl acpi = new AddCallPlanItemControl();
        acpi.CallPlan = cp;
        acpi.IsCheck = true;
        
        AddCallPlanItemControl acpi1 = new AddCallPlanItemControl();
        acpi1.CallPlan = cp1;
        acpi1.IsCheck = true;
   
        
        AddCallPlanItemControl acpi2 = new AddCallPlanItemControl();
        acpi2.CallPlan = cp2;
        acpi2.IsCheck = true;
        
        AddCallPlanItemControl acpi3 = new AddCallPlanItemControl();
        acpi3.CallPlan = cp3;
        acpi3.IsCheck = true;
        
        AddCallPlanItemControl acpi4 = new AddCallPlanItemControl();
        acpi4.CallPlan = cp4;
        acpi4.IsCheck = true;

        AddCallPlanItemControl acpi5 = new AddCallPlanItemControl();
        acpi5.CallPlan = cp5;
        acpi5.IsCheck = true;        
         /* create by xiaojiang 100916*/
         AddCallPlanItemControl acpi6 = new AddCallPlanItemControl();
        acpi6.CallPlan = cp6;
        acpi6.IsCheck = true;  
         AddCallPlanItemControl acpi7 = new AddCallPlanItemControl();
        acpi7.CallPlan = cp7;
        acpi7.IsCheck = true;  
        
          AddCallPlanItemControl acpi8 = new AddCallPlanItemControl();
        acpi8.CallPlan = cp8;
        acpi8.IsCheck = true;  
         /*end create*/
        
        List<AddCallPlanItemControl> cpl = new List<AddCallPlanItemControl>();
     
        cpl.add(acpi);
        
        List<AddCallPlanItemControl> cpl2 = new List<AddCallPlanItemControl>();
      
        cpl2.add(acpi1); 
        
        List<AddCallPlanItemControl> cpl3 = new List<AddCallPlanItemControl>();
       
        cpl3.add(acpi2); 
        
        List<AddCallPlanItemControl> cpl4 = new List<AddCallPlanItemControl>();
       
       /* cpl3.add(acpi3);  modified by xiaojiang 100913 */
        cpl4.add(acpi3);
       
        
        List<AddCallPlanItemControl> cpl5 = new List<AddCallPlanItemControl>();
       
       /* cpl3.add(acpi4);  modified by xiaojiang 100913 */
       cpl5.add(acpi4);
        
        List<AddCallPlanItemControl> cpl6 = new List<AddCallPlanItemControl>();

        /*cpl3.add(acpi5); modified by xiaojiang 100913 */
          cpl6.add(acpi5);     
        /* create by xiaojiang 100916*/
         List<AddCallPlanItemControl> cpl7 = new List<AddCallPlanItemControl>();
         List<AddCallPlanItemControl> cpl8 = new List<AddCallPlanItemControl>();
         List<AddCallPlanItemControl> cpl9 = new List<AddCallPlanItemControl>();
          cpl7.add(acpi6); 
           cpl8.add(acpi7); 
           cpl9.add(acpi8); 
         /*end create*/

        AddCallPlanControl testCase_S = new AddCallPlanControl();
        
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 
        
        testCase_S.opflag = '1';
        testCase_S.OtherList = cpl;   
        testCase_S.SaveAndNew();
        testCase_S.CallPlanList = cpl6;   
        testCase_S.SaveAndNew();
        testCase_S.OtherList = cpl2;  
        //testCase_s.SaveOutPlan();
        testCase_S.CallPlanList = cpl5;
          
        testCase_s.SaveOutPlan();

        testCase_S.OtherList = cpl3; 
        testCase_S.UrlId=null;//modified by xiaojiang 100914 
       // testCase_S.Save();
        testCase_S.CallPlanList = cpl4; 
        testCase_S.UrlId=null; //modified by xiaojiang 100914
        testCase_S.Save();
        testCase_S.Cancel();

      /************************/
        AddCallPlanControl testCaseA = new AddCallPlanControl();
        
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp1.Id); 
        
        testCaseA.OtherList = cpl;    
        testCaseA.DeleteOther();
        testCaseA.CallPlanList = cpl; 
        
        AddHCPItemControl hcpi1 = new AddHCPItemControl(hcp);
        hcpi1.IsCheck = true;
        testCaseA.HCPItemList = new List<AddHCPItemControl>();  
        testCaseA.HCPItemList.add(hcpi1);
        System.assert(testCaseA.options != null);
        testCaseA.UpdateRecord();       
        testCaseA.CallPlanList = cpl; 
         
      
      /**************************/
                           
        AddCallPlanControl testCase = new AddCallPlanControl();
        testCase_S.OtherList = cpl;
        testCase.SaveOutPlan();
               
        System.assert(testCase.WorkType != null);
        System.assert(testCase.WorkType != null);
        System.assert(testCase.IdList != null);
        System.assert(testCase.options != null);
        testCase.wtSelect = '3';
         System.assert(testCase.options != null);
        System.assert(testCase.RTOptions != null);
        System.assert(testCase.RTOptions != null);
        System.assert(testCase.cpDate != null);
        System.assert(testCase.opflag == null);
        System.assert(testCase.cpDate != null);
        
        testCase.wkString = '拜访';
        testCase.wtSelect = '2';
        testCase.IdSelect = hos.Id;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCase.actionSupport();
        testCase.HCPItemList.get(0).IsCheck = true;
        testCase.CallPlanList = cpl;
              
        testCase.UpdateRecord();
        testCase.wtSelect = '3';
        testCase.UpdateRecord();
        testCase.wtSelect = '4';
        testCase.UpdateRecord();
        testCase.wtSelect = '31';
        testCase.UpdateRecord();
        testCase.wtSelect = '41';
        testCase.UpdateRecord();

        testCase.wtSelect = '5';
        testCase.UpdateRecord();
        testCase.wtSelect = '6';
        testCase.UpdateRecord();
        testCase.wtSelect = '7';
        testCase.UpdateRecord();
        testCase.wtSelect = '8';
        testCase.UpdateRecord();
        testCase.wtSelect = null;
        testCase.UpdateRecord();
        testCase.wtSelect = '9';
        testCase.UpdateRecord();

        testCase.CallPlanList = cpl2;
        testCase.CallPlanList.get(0).CallPlan.CallPlanTime__c = 'AllDay';

        testCase.Cancel();
        
        testCase.wtSelect = '18';
        testCase.wkString = 'aaa';
        testCase.OtherList = null;
        testCase.wkString = '本地消费者教育活动';
        AddHCPItemControl hcpi = new AddHCPItemControl(hcp);
        hcpi.IsCheck = true;
        testCase.HCPItemList = new List<AddHCPItemControl>();  
        testCase.HCPItemList.add(hcpi);
        testCase.UpdateRecord();    
        testCase.DeleteRecord();

        testCase.opflag = '1';
        testCase.CallPlanList = cpl;  
        testCase.UrlId =null;//modify by xiaojiang 100914 
        testCase.Save();
        testCase.opflag = null;
        testCase.CancelOutPlan();
        
      /**************************/
                           
        AddCallPlanControl testCaseB = new AddCallPlanControl();
               
        System.assert(testCaseB .WorkType != null);
        System.assert(testCaseB .IdList != null);
        System.assert(testCaseB .options != null);
        System.assert(testCaseB .RTOptions != null);
        System.assert(testCaseB .cpDate != null);
        System.assert(testCaseB .opflag == null);
        System.assert(testCaseB .cpDate != null);
        
        testCaseB .wkString = '拜访';
        testCaseB .wtSelect = '2';
        testCaseB .IdSelect = hos.Id;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseB .actionSupport();
        testCaseB .HCPItemList.get(0).IsCheck = true;
        testCaseB .CallPlanList = cpl;
              
        testCaseB .UpdateRecord();
        testCaseB .wtSelect = '3';
        testCaseB .UpdateRecord();


        testCaseB .Cancel();
        
        testCaseB .wtSelect = '18';
        testCaseB .wkString = 'aaa';
        testCaseB .OtherList = null;
        testCaseB .wkString = '本地消费者教育活动';
        AddHCPItemControl hcpi5 = new AddHCPItemControl(hcp);
        hcpi5.IsCheck = true;
        testCaseB .HCPItemList = new List<AddHCPItemControl>();  
        testCaseB .HCPItemList.add(hcpi);
        testCaseB .UpdateRecord();    
        testCaseB .DeleteRecord();

       testCaseB .CallPlanList = cpl4;
       //testCaseB .SaveOutPlan();  
        
        testCaseB .cpDate.CallPlanDate__c = null;
        testCaseB .CallPlanList = null;
        testCaseB.UrlId=null;//modified by xiaojiang 100914
        testCaseB.Save();
       testCaseB .CallPlanList = cpl4;
       testCaseB.UrlId=null;//modified by xiaojiang 100914
       testCaseB.Save();

       testCaseB .CallPlanList = null;
       testCaseB.SaveAndNew();  
       
       testCaseB .CallPlanList = cpl4;
       testCaseB.SaveAndNew();

        /*create by xiaojiang 100914*/
          AddCallPlanControl testCaseC = new AddCallPlanControl();
               
        System.assert(testCaseC .WorkType != null);
        System.assert(testCaseC .IdList != null);
        System.assert(testCaseC .options != null);
        System.assert(testCaseC .RTOptions != null);
        System.assert(testCaseC .cpDate != null);
        System.assert(testCaseC .opflag == null);
        System.assert(testCaseC .cpDate != null);
        
        testCaseC .wkString = '拜访';
        testCaseC .wtSelect = '2';
        testCaseC .IdSelect = hos.Id;
        testCaseC .returnFlag=true;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseC.OtherList = cpl;   
        testCaseC .cpDate.CallPlanDate__c = date.today().addDays(2);
        testCaseC .CallPlanList = cpl6;
        testCaseC .Save();
        
      
        /*end create*/
        
            /*create by xiaojiang 100916*/
          AddCallPlanControl testCaseD = new AddCallPlanControl();
               
        System.assert(testCaseD .WorkType != null);
        System.assert(testCaseD .IdList != null);
        System.assert(testCaseD .options != null);
        System.assert(testCaseD .RTOptions != null);
        System.assert(testCaseD .cpDate != null);
        System.assert(testCaseD .opflag == null);
        System.assert(testCaseD .cpDate != null);
        
        testCaseD .wkString = '拜访';
        testCaseD .wtSelect = '2';
        testCaseD .IdSelect = hos.Id;
        testCaseD .returnFlag=true;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseD .OtherList = cpl5;   
        testCaseD .cpDate.CallPlanDate__c = date.today();
        testCaseD .CallPlanList = cpl6;
        testCaseD .Save();
         testCaseD.SaveAndNew();
      
      
         AddCallPlanControl testCaseE = new AddCallPlanControl();
               
        System.assert(testCaseE .WorkType != null);
        System.assert(testCaseE .IdList != null);
        System.assert(testCaseE .options != null);
        System.assert(testCaseE .RTOptions != null);
        System.assert(testCaseE .cpDate != null);
        System.assert(testCaseE .opflag == null);
        System.assert(testCaseE .cpDate != null);
        
        testCaseE .wkString = '拜访';
        testCaseE .wtSelect = '2';
        testCaseE .IdSelect = hos.Id;
        testCaseE .returnFlag=true;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseE .OtherList = cpl5;   
        testCaseE .cpDate.CallPlanDate__c = date.today().addDays(2);
        testCaseE .CallPlanList = cpl7;
        //testCaseE .Save();
         //testCaseE.SaveAndNew();
        /*
          AddCallPlanControl testCaseF = new AddCallPlanControl();
               
        System.assert(testCaseF.WorkType != null);
        System.assert(testCaseF.IdList != null);
        System.assert(testCaseF.options != null);
        System.assert(testCaseF.RTOptions != null);
        System.assert(testCaseF.cpDate != null);
        System.assert(testCaseF.opflag == null);
        System.assert(testCaseF.cpDate != null);
        
        testCaseF.wkString = '拜访';
        testCaseF.wtSelect = '2';
        testCaseF.IdSelect = hos.Id;
        testCaseF.returnFlag=true;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseF.OtherList = cpl7;   
        testCaseF.cpDate.CallPlanDate__c = date.today().addDays(2);
        testCaseF.CallPlanList = cpl8;
        testCaseF.Save();
         testCaseF.SaveAndNew();
        testCaseF.UpdateRecord();    
        testCaseF.DeleteRecord();
        
          AddCallPlanControl testCaseG = new AddCallPlanControl();
               
        System.assert(testCaseG.WorkType != null);
        System.assert(testCaseG.IdList != null);
        System.assert(testCaseG.options != null);
        System.assert(testCaseG.RTOptions != null);
        System.assert(testCaseG.cpDate != null);
        System.assert(testCaseG.opflag == null);
        System.assert(testCaseG.cpDate != null);
        
        testCaseG.wkString = '拜访';
        testCaseG.wtSelect = '2';
        testCaseG.IdSelect = hos.Id;
        testCaseG.returnFlag=true;
        test.setCurrentPageReference(Page.AddCallPlan);
        System.currentPageReference().getParameters().put('id', mcp.Id); 

        testCaseG.OtherList = cpl8;   
        testCaseG.cpDate.CallPlanDate__c = date.today().addDays(2);
        testCaseG.CallPlanList = cpl9;
        testCaseG.Save();
        testCaseG.SaveAndNew();
    
        */
      } 
        
    }
    
}